# -*- coding: utf-8 -*-

###################
#   miniTH
#   This program serves for serial change of photos size, to put watermarks
#   and at the same time for making thumbnails.
#   It can be useful for quick processing of big amount of pictures in order to put
#   them in web galleries or on websites.
#   Copyright (C) 2011 DooBLER
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
###################

###################
# svn: $Id: myFileListModel.py 19 2011-08-15 13:41:38Z MrDooBLER@gmail.com $
# Author: DooBLER http://doobler.net
###################

import sys

from PySide import QtCore, QtGui

class myFileListModel(QtCore.QAbstractListModel): 
    
    numberPopulated = QtCore.Signal(int)
    
    def __init__(self, parent=None): 
        QtCore.QAbstractListModel.__init__(self, parent) 
        self.fileCount=0     
        self.fileList=[]    #initialize the file list as a Python list. 
        
        self._filters = (QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self._nameFilters = []
        self._rootPath = None
    
    
    def setNameFilters(self, filters):
        self._nameFilters = list(filters)
        self._fileListReload()
        
    
    def _fileListReload(self):
        self.fileList = self._rootPath.entryInfoList(self._nameFilters, filters = self._filters) 
        
        self.fileCount=0 
        self.reset() 
    
    
    def filePath(self, index):
        """
        Funkcja zwraca path do pliku wraz z nazwą pliku
        """
        return self.fileList[index.row()].absoluteFilePath()
    
    
    def rowCount(self, parent): 
        """ 
        parent=QModelIndex 
        """ 
        return self.fileCount 
    
    
    def data(self, index, role=QtCore.Qt.DisplayRole): 
        """ 
        index=QModelIndex 
        """ 
        if not index.isValid(): 
            return None
        
        if index.row()>=len(self.fileList) or index.row()<0: 
            return None 
        
        if role==QtCore.Qt.DisplayRole: 
            return self.fileList[index.row()].fileName()
        elif role==QtCore.Qt.BackgroundRole: 
            batch=(index.row()/100)%2 
            if batch==0: 
                return QtGui.qApp.palette().base()
            else: 
                return QtGui.qApp.palette().alternateBase()
        
        return None
    
    
    def canFetchMore(self, index): 
        """ 
        index=QModelIndex 
        """ 
        if self.fileCount < len(self.fileList): 
            return True 
        else: 
            return False 
     
    
    def fetchMore(self, index): 
        """ 
        Index=QModelIndex 
        """ 
        
        remainder=len(self.fileList)-self.fileCount 
        itemsToFetch=min(100, remainder) 
        
        self.beginInsertRows(QtCore.QModelIndex(), self.fileCount, self.fileCount+itemsToFetch) 
        
        self.fileCount+=itemsToFetch 
        
        self.endInsertRows() 
        
        # self.emit(QtCore.SIGNAL("numberPopulated"), itemsToFetch) 
        
        self.numberPopulated.emit(itemsToFetch)
    
    
    def setRootPath(self, path): 
        self._rootPath = QtCore.QDir(path)
        
        self._fileListReload()
        
    

